import http from '../../apis/api.js';
import util from '../../utils/util.js';
let self 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wechatRepairShow: false,/**是否展示报修单处理详情 */
    dealRepair: false,/**是否展示故障单处理 */
    addComponents: false,/**是否展示换上的零件或备件列表 */
    uninstallMaterial: false,/**是否展示换上的零件或备件列表 */
    id:'',
    detail:null,
    isJyUser: false,
    formData:{
      dealRemark:null,
      dealResult:1,
      dealer:null,
      dealerName:''
    },
    error:{
      dealerName:''
    },
    repairPersonnel:[],/***维修人员列表 */
    submitDisable:false,
    selfRepairDetail:null,
    sendRepairDetail:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    self = this;
    self.data.id = options.id;
    util.getRole().then(res => {
      self.data.roleList = res;
      self.data.isJyUser = res.indexOf('jy_user') > -1;
      self.setData({
        isJyUser: self.data.isJyUser
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    self.getRepaitDetail();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  /**获取报账单详情*/
  getRepaitDetail(){
    http.repairDetail(self.data.id).then(res=>{
      self.data.detail=res.content;
      console.log(self.data.detail);
      self.setData({
        detail: self.data.detail
      })
      self.getRepairPersonnel();
      if (self.data.detail.dealResult===1){
        self.getSelfRepairDetail();
      } else if (self.data.detail.dealResult === 2){
        self.getSendRepairDetail();
      }
    })
  },
  /**获取自修详情*/
  getSelfRepairDetail(){
    http.getSelfRepairDetail(self.data.id).then(res=>{
      self.data.selfRepairDetail=res.content;
      self.setData({
        selfRepairDetail:self.data.selfRepairDetail
      })
    })
  }, 
  /**获取派修详情*/
  getSendRepairDetail(){
    http.getSendRepairDetail(self.data.id).then(res => {
      self.data.sendRepairDetail = res.content;
      self.setData({
        sendRepairDetail: self.data.sendRepairDetail
      })
    })
  },
  /**点击选择处理结果 */
  selectDealResult(e){
    self.data.formData.dealResult = Number(e.currentTarget.dataset.itemvalue);
    self.data.formData.dealer = null;
    self.data.formData.dealerName = '';
    self.setData({
      formData: self.data.formData
    })
    self.getRepairPersonnel();
  },
  /**获取故障处理人*/
  getRepairPersonnel(){
    if (self.data.formData.dealResult!=='3'){
      let roleId = self.data.formData.dealResult === '1' ? '3' :'4';
        http.repairPersonnel({
          pid: self.data.detail.id,
          roleId,
        }).then(res => {
          self.data.repairPersonnel = res.content;
          self.setData({
            repairPersonnel: self.data.repairPersonnel
          })
        })
    }
  },
  bindDealerChange(e){
    e.detail.value
    self.data.formData.dealer = self.data.repairPersonnel[e.detail.value].id;
    self.data.formData.dealerName = self.data.repairPersonnel[e.detail.value].name;
    self.setData({
      formData: self.data.formData
    })
  },
  /**切换显示隐藏详情*/
  showDetail(e) {
    let opt = e.currentTarget.dataset;
    switch (opt.type) {
      case 'wechatRepairShow':
        self.data.wechatRepairShow = !self.data.wechatRepairShow;
        self.setData({
          wechatRepairShow: self.data.wechatRepairShow
        })
        break;
      case 'dealRepair':
        self.data.dealRepair = !self.data.dealRepair;
        self.setData({
          dealRepair: self.data.dealRepair
        })
        break;
      case 'addComponents':
        self.data.addComponents = !self.data.addComponents;
        self.setData({
          addComponents: self.data.addComponents
        })
        break;
      case "uninstallMaterial":
        self.data.uninstallMaterial = !self.data.uninstallMaterial;
        self.setData({
          uninstallMaterial: self.data.uninstallMaterial
        })
        break;
    }
  },
  /**获取input框内容*/ 
  getValus(e){
    let type = e.currentTarget.dataset.type;
    switch (type) {
      case 'dealRemark':
        self.data.formData.dealRemark = e.detail.value;
        break;
    }
    self.setData({
      formData: self.data.formData
    })
  },
  checked(name) {
    /**表单输入验证 */
    switch (name) {
      case 'dealerName':
        if (self.data.formData.dealerName === '' && self.data.formData.dealResult!=='3') {
          self.data.error.dealerName = "请选择故障处理人"
          return false
        }
        self.data.error.dealerName = '';
        return true;
        break;
    }
  },
  submitInfo(){
    self.setData({
      submitDisable: true
    })
    if (self.checked('dealerName')) {
      let postData = self.data.detail;
      postData.dealRemark = self.data.formData.dealRemark;
      postData.dealResult = self.data.formData.dealResult;
      postData.dealer = self.data.formData.dealer;
      http.updateRepair(postData).then(res => {
        wx.showToast({
          title: '报修成功',
          icon: 'success',
          duration: 2000,
          success() {
            self.getRepaitDetail();
          },
          complete() {
            self.setData({
              submitDisable: false
            })
          }
        })
      }, error => {
        self.setData({
          submitDisable: false
        })
      })
    } else {
      self.setData({
        error: self.data.error,
        submitDisable: false
      })
    }
  }
})